<template>
  <header>
    <div class="banxin tanxin head_in">
      <section>欢迎来到叩叮狼积分商城</section>
      <ul>
        <li class="avatar_li tanxin">
          <img width="26" class="avatar" :src="userInfo.headImg" alt="">
          <a>用户名：{{ userInfo.nickName }}</a>
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩叮狼官网</li>
        <li class="head_btn cart" v-if="isLogin" @click="$router.push('/user/cart')">
          <img src="../assets/images/Shopping.png" alt="" />
          <span>购物车</span>
          <b>{{ cartTotal }}</b>
        </li>
        <li class="head_btn login" v-else @click="goLogin">登录</li>
      </ul>
    </div>
  </header>
</template>

<script>
import { UserInfoApi, WeiXinLoginApi} from '@/request/api';
export default {
  data() {
    return {
      isLogin: false,
      userInfo: {
        // 头像
        headImg: require("../assets/images/userImg.png"),
        // 名字
        nickName: "游客",
        // 鸡腿数
        coin: "--",
        code: '',
      },
      // 购物出数量
      cartTotal: this.$store.state.cartTotal,
    };
  },
  methods: {
    goLogin() {
      this.$store.commit("changeLoginShow", true)
    },
    bindWecahtFn() {
      console.log(this.$route.query.code);
      let code = this.$route.query.code;
      if (code) {
        WeiXinLoginApi({ code }).then((res) => {
          if (res.data.code === 0) {
            let obj = {
              content: "登录成功",
              icon: "success",
            };
            this.$store.dispatch("AsyncToast", obj);
            // 存储token
            localStorage.setItem("x-auth-token", res.data["x-auth-token"])
            // 更新header组件
            this.$emit('updateKeyFn')
            // 删除地址栏code
            this.$router.push(this.$route.path)
          } else if (res.data.code === 400) {
            let obj = {
              content: "页面过期，请重新扫码二维码",
              icon: "danger",
            };
            this.$store.dispatch("AsyncToast", obj);
            // 重新打开登录框
            this.$store.commit('changeLoginShow', true)
          } if (res.data.code === 407) {
            // 需要手机验证码登录
            let obj = {
              content: "请使用手机号登录来绑定微信",
              icon: "danger"
            }
            this.$store.dispatch('AsyncToast', obj)
            // 重新打开登录框
            this.$store.commit('changeLoginShow', true)
            // 临时存储下uuid
            sessionStorage.setItem('uuid', res.data.uuid)
          } else {

          }
        })
      }
    }
  },
  watch: {
    "$route.query.code":{
      handler(newVal,oldVal){
        if(newVal !== oldVal){
          this.bindWecahtFn();
        }
      }
    }
  },
  created() {
    // 获取用户信息
    UserInfoApi().then((res) => {
      if (res.data.code === 0) {
        let { cartTotal, userInfo } = res.data.data;
        this.cartTotal = cartTotal;
        this.userInfo = userInfo;
        this.isLogin = true;
      }
    });
    // 调用绑定微信的函数
    this.bindWecahtFn();
  }
}
</script>
 
<style lang = "less" scoped>
@import "../assets/base.less";

header {
  background: @black;
  height: 40px;
  color: #fffefe;
  font-size: 14px;

  .head_in {
    align-items: center;
    height: 40px;

    ul {
      display: flex;
      height: 40px;
      align-items: center;

      li {
        margin-right: 20px;

        a {
          text-decoration: none;
          color: #fffefe;
          margin-left: 5px;
          margin-top: 5px;
        }

        &.head_btn {
          width: 124px;
          height: 40px;
          background: @skyblue;
          cursor: pointer;
        }

        &.login {
          text-align: center;
          line-height: 40px;
        }

        &.cart {
          display: flex;
          align-items: center;
          justify-content: center;

          span {
            margin-left: 8px;
            margin-right: 6px;
          }

          b {
            width: 22px;
            height: 22px;
            background: @orange;
            border-radius: 11px;
            text-align: center;
            line-height: 22px;
          }
        }
      }
    }
  }
}
</style>